<template>
  <div class="home-page">
    <!-- Hero Section -->
    <section class="hero">
      <div class="container">
        <h1 class="hero-title">欢迎来到我的个人博客</h1>
        <p class="hero-subtitle">在这里分享技术、思考和生活点滴</p>
        <router-link to="/posts" class="hero-button">浏览所有文章</router-link>
      </div>
    </section>

    <!-- Latest Posts -->
    <section class="latest-posts section">
      <div class="container">
        <h2 class="section-title">最新文章</h2>
        <div class="posts-grid">
          <PostCard 
            v-for="post in latestPosts" 
            :key="post.id" 
            :post="post"
          />
        </div>
        <!-- 最近更新模块 -->
        <div class="recent-updates">
          <h3 class="section-subtitle">最新更新</h3>
          <div class="updates-list">
            <div class="update-item" v-for="update in recentUpdates" :key="update.id">
              <span class="update-date" v-text="update.date"></span>
              <router-link :to="`/post/${update.id}`" class="update-title" v-text="update.title"></router-link>
            </div>
          </div>
        </div>
        <div class="section-footer">
          <router-link to="/posts" class="view-all-link">查看所有文章 →</router-link>
        </div>
      </div>
    </section>

    <!-- Featured Posts -->
    <section class="featured-posts section">
      <div class="container">
        <h2 class="section-title">热门推荐</h2>
        <div class="posts-grid">
          <PostCard 
            v-for="post in featuredPosts" 
            :key="post.id" 
            :post="post"
          />
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import PostCard from '@/components/PostCard.vue'
import { allPosts } from '../utils/mock.js'

export default {
  name: 'HomePage',
  components: {
    PostCard
  },
  data() {
    return {
      // 按日期排序，获取最新的3篇文章作为最新文章
      latestPosts: [...allPosts]
        .sort((a, b) => new Date(b.date) - new Date(a.date))
        .slice(0, 3)
        .map(post => ({
          ...post,
          // 为了保持与现有代码结构一致，确保返回需要的字段
          id: post.id,
          title: post.title,
          excerpt: post.excerpt,
          date: post.date,
          category: post.category,
          tags: post.tags,
          image: post.image
        })),
      // 精选文章（这里我们选择id为4和5的文章，与原有数据保持一致）
      featuredPosts: allPosts.filter(post => post.id === 4 || post.id === 5).map(post => ({
        ...post,
        id: post.id,
        title: post.title,
        excerpt: post.excerpt,
        date: post.date,
        category: post.category,
        tags: post.tags,
        image: post.image
      })),
      // 最近更新（按日期排序的最新3篇文章）
      recentUpdates: [...allPosts]
        .sort((a, b) => new Date(b.date) - new Date(a.date))
        .slice(0, 3)
        .map(post => ({
          id: post.id,
          title: post.title,
          date: post.date
        }))
    }
  },
  methods: {
    formatDate(dateString) {
      const options = { year: 'numeric', month: 'long', day: 'numeric' }
      return new Date(dateString).toLocaleDateString('zh-CN', options)
    }
  }
}
</script>

<style scoped>
.home-page {
  width: 100%;
}

/* Hero Section */
.hero {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 5rem 0;
  text-align: center;
  border-radius: 0 0 20px 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.hero-title {
  font-size: 3rem;
  margin-bottom: 1rem;
  font-weight: 800;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.hero-subtitle {
  font-size: 1.3rem;
  margin-bottom: 2.5rem;
  opacity: 0.95;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.hero-button {
  display: inline-block;
  background: rgba(255, 255, 255, 0.9);
  color: #667eea;
  padding: 1rem 2rem;
  border-radius: 50px;
  text-decoration: none;
  font-weight: 700;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  border: 2px solid white;
}

.hero-button:hover {
  background: transparent;
  color: white;
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

/* Sections */
.section {
  padding: 4rem 0;
}

.section-title {
  text-align: center;
  font-size: 2rem;
  margin-bottom: 3rem;
  position: relative;
  color: #333;
}

.section-title::after {
  content: '';
  display: block;
  width: 60px;
  height: 4px;
  background: linear-gradient(to right, #667eea, #764ba2);
  margin: 0.5rem auto;
  border-radius: 2px;
}

/* Posts Grid */
.posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

/* Post Card */
.post-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.post-image img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.post-content {
  padding: 1.5rem;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: #666;
}

.post-category {
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  padding: 0.2rem 0.8rem;
  border-radius: 20px;
  font-weight: 600;
}

.post-title {
  margin-bottom: 1rem;
  font-size: 1.3rem;
}

.post-title a {
  text-decoration: none;
  color: #333;
  transition: color 0.3s;
}

.post-title a:hover {
  color: #667eea;
}

.post-excerpt {
  color: #666;
  margin-bottom: 1.5rem;
  line-height: 1.6;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  background: #f0f2f5;
  color: #666;
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  font-size: 0.8rem;
  transition: all 0.3s;
}

.tag:hover {
  background: #667eea;
  color: white;
}

.featured-card {
  border-top: 4px solid #667eea;
}

.section-footer {
  text-align: center;
  margin-top: 2rem;
}

.view-all-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  padding: 0.5rem 1rem;
  border-radius: 30px;
  transition: all 0.3s;
  border: 2px solid #667eea;
}

.view-all-link:hover {
  background: #667eea;
  color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .hero {
    padding: 3rem 0;
  }
  
  .hero-title {
    font-size: 2.2rem;
  }
  
  .hero-subtitle {
    font-size: 1.1rem;
  }
  
  .posts-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
  
  .section {
    padding: 2rem 0;
  }
  
  .section-title {
    font-size: 1.7rem;
  }
}
</style>